// posts.js
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');

Page({
  onShareAppMessage: function () {
    return this.data.shareAppMessage;
  },
  data: {
    title: '话题列表',
    postsList: [],
    hidden: false,
    page: 1,
    tab: 'all',
    loading: false,
    shareAppMessage: {}
  },
  onLoad: function () {
    this.data.shareAppMessage = util.getShareAppMessage();
    this.fetchData();
  },
  onPullDownRefresh: function () {
    console.log('下拉刷新', new Date());
    this.data.page = 1;
    console.log(this.data.page);
    this.fetchData();
  },
  onReachBottom: function () {
    console.log('上拉加载更多', new Date());
    console.log(this.data.page);
    this.fetchData();
  },
  onTapTag: function (e) {
    var self = this;
    var tab = e.currentTarget.id;
    self.setData({
      tab: tab
    });
    if (tab !== 'all') {
      this.fetchData({ tab: tab });
    } else {
      this.fetchData();
    }
  },
  fetchData: function (data) {
    var self = this;
    if (self.data.loading) {
      return;
    }
    self.data.loading = true;
    if (!data) data = {};
    if (!data.page) data.page = 1;
    if (self.data.page === 1) {
      self.setData({
        postsList: []
      });
      self.setData({
        hidden: false
      });
    } else {
      wx.showNavigationBarLoading();
    }
    data.page = self.data.page;
    data.userId = util.getUserId();
    wx.request({
      url: Api.getTrials(data),
      header: Api.headers,
      success: function (res) {

        console.log("fetchData" + "-" + JSON.stringify(res));
        console.log("data" + !res.data.data);
        if (res.data.code == 0 || res.data.code == 1) {
          //   wx.showToast({
          //   title: res.data.msg,
          //   duration: 1500
          // })
        } else if (res.data.code != 1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          return
        } else if (!res.data.data || res.data.data.length < 1) {
          wx.showToast({
            title: res.data.msg,
            duration: 1500
          })
          return;
        }
        // if (res.data.code != 1) {
        //   wx.showModal({
        //     title: '提示',
        //     content: res.data.msg,
        //     showCancel: false,
        //     success: function (res) {
        //       if (res.confirm) {
        //         console.log('用户点击确定')
        //       }
        //     }
        //   });
        //   return
        // } else if (!res.data.data || res.data.data.length < 1) {
        //   wx.showToast({
        //     title: res.data.msg,
        //     duration: 1500
        //   })
        //   return;
        // }
        self.setData({
          postsList: self.data.postsList.concat(res.data.data.map(function (item) {
            item.starttime = util.build_time(item.starttime);
            item.endtime = util.build_time(item.endtime);
            return item;
          }))
        });
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
        self.data.page++;
      },
      fail: function (res) {
        console.log('fail - ' + JSON.stringify(res));
      },
      complete: function (res) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh()
        self.data.loading = false;
        console.log('complete - ' + JSON.stringify(res));
      },
    });
  },
  redictDetail: function (e) {
    var id = e.currentTarget.id,
      url = '../detail/detail?id=' + id;
    console.log('试用详情' + id);
    wx.navigateTo({
      url: url
    })
  },
  lower: function (e) {
    var self = this;
    self.setData({
      page: self.data.page + 1
    });
    if (self.data.tab !== 'all') {
      this.fetchData({ tab: self.data.tab, page: self.data.page });
    } else {
      this.fetchData({ page: self.data.page });
    }
  },
  //申请试用
  applyTrial: function (e) {
    if (util.checkLogin("", "phone")) {
      var isApply = e.target.dataset.apply;
      console.log('isApply - ' + isApply);
      if (isApply) {
        wx.showModal({
          title: '提示',
          content: "您已经提交过申请了",
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
        return;
      }
      var id = e.currentTarget.id,
        title = e.target.dataset.title,
        url = '../apply/apply?id=' + id + '&title=' + title;
      console.log('申请试用' + id + "-" + title);
      wx.navigateTo({
        url: url
      });
    }
  },
})
